<?php
require_once('includes/include.php');
$delete = new delete();
$root = ($_SERVER['DOCUMENT_ROOT'])."/etam/apitest";
$files = $delete->readDirectory($root,'.svn',true,true);
$finded = $delete->allSerched;
foreach($finded as $folder){
	var_dump($folder);
	//$delete->deleteFile($folder);
}
var_dump($delete->message);
/* function deleteFile($dir){
	$path = $dir;
	if(is_dir($dir)){
		$dirTemp = opendir($dir);
		while(($file = readdir($dirTemp)) !== false){
			$tempFile = $path."/".$file;
			if(is_dir($tempFile)&&$file!="."&&$file!=".."){
				deleteFile($tempFile);
				
			}else if(!is_dir($tempFile)&&$file!="."&&$file!=".."){
				$modeChange = chmod($tempFile,0777);
				unlink($tempFile);
			}
		}
		closeDir($dirTemp);
		rmdir($dir);
	}
} */
class delete{
	public $files;
	public $allSerched=array();
	public $message=array();
	public function readDirectory($dir='',$findFile='',$all=false,$absolute=false){
		$baseDir=$this->getBaseDir($dir,$absolute);
		if(is_dir($baseDir)){
			$finded = $this->getDirectoryFile($baseDir,$findFile,$all);
			if($finded!=''&&!is_array($finded)&&$findFile){			
				return $finded;
			}else if($finded!=''&&is_array($finded)&&!$findFile){
				return $finded;
			}
		}
	}
	public function getDirectoryFile($dir,$findFile='',$all=false){
		$path=$dir."/";
		  // var_dump("path:::".$path);
		 $targetFinded='';
		if(is_dir($dir)){
			$dirTemp = opendir($dir);
			while (($file = readdir($dirTemp)) !== false){
				$tempFile=$path.$file;
				if(!is_dir($tempFile)&&$file!="."&&$file!=".."&&$file!=".svn"&&!$findFile){
					if(isset($this->files)){
						$this->files[]=$tempFile;
					}else{
						$this->files=array($tempFile);
					}
				}
				if($findFile==$file){
					$targetFinded=$path.$findFile;
					if (!$all){
						break;
					}else{
						if(!isset($this->allSerched)){
							$this->allSerched=array();
						}
						$this->allSerched[]=$targetFinded;
					}
				}
				if(is_dir($tempFile)&&$file!="."&&$file!=".."&&$file!=".svn"){
					if(isset($this->files)){
						$this->files[]=$tempFile."/";
					}else{
						$this->files=array(($tempFile."/"));
					}
					$childDir = $path.$file;
					$targetFinded = $this->getDirectoryFile($childDir,$findFile,$all);
					if($targetFinded&&!$all){
						return $targetFinded;
					}
				}
			}
			closedir($dirTemp);
		}
		if($targetFinded){
			return $targetFinded;
		}else if(isset($this->files)&&!$findFile){
			return $this->files;
		}
	}
	/**
	*param $dir file directory path(delete the directory) or file path(delete FILE)
	*/
	public function deleteFile($dir){
		$path = $dir;
		if(is_dir($dir)){
			$dirTemp = opendir($dir);
			while(($file = readdir($dirTemp)) !== false){
				$tempFile = $path."/".$file;
				if(is_dir($tempFile)&&$file!="."&&$file!=".."){
					$this->deleteFile($tempFile);
					
				}else if(!is_dir($tempFile)&&$file!="."&&$file!=".."){
					$modeChange = chmod($tempFile,0777);
					unlink($tempFile);
					$this->message[]="deleted File '$tempFile'";
				}
			}
			closeDir($dirTemp);
			rmdir($dir);
			$this->message[]="delete Folder '$dir'";
		}else{
			if(file_exists($path)){
				$modeChange = chmod($path,0777);
				unlink($path);
				$this->message[]="deleted File '$path'";
			}else{
				$this->message[]="no File exist '$path'";
			}
		}
	}
	public function getBaseDir($dir='',$absolute=false){
		if($dir==''){
			$currentScript = $_SERVER['PHP_SELF'];
			$currentFileArray = explode("/",$currentScript);
			if(count($currentFileArray)!=2){
				$baseDir =$_SERVER['DOCUMENT_ROOT']."/".$currentFileArray[1];
			}else{
				$baseDir=$_SERVER['DOCUMENT_ROOT'];
			}
			$baseDir=$this->prepareDir($baseDir);
		}else if($dir&&!$absolute){
			$baseDir=$this->getBaseDir()."/".$dir;
			$baseDir=$this->prepareDir($baseDir);
		}else{
			$baseDir=$this->prepareDir($dir);
		}
		return $baseDir;
	}
	public function getRootDir(){
		return prepareDir($_SERVER['DOCUMENT_ROOT']);
	}
	public function prepareDir($dir){
		if (stripos($dir,"//",0)){
			$replecePattern ="|\/\/|";
			$dir=preg_replace($replecePattern,'/',$dir);
		}
		if (stripos($dir,"\\",0)){
			$replecePattern ="|\\\\|";
			$dir=preg_replace($replecePattern,'/',$dir);
		}
		return $dir;
	}
}
?>